package com.jy.oms.framework.asyn.task.util;

import com.jy.oms.framework.asyn.task.constants.TaskConstants;
import com.jy.oms.framework.asyn.task.constants.TaskExecuteStrategyEnum;
import com.jy.oms.framework.asyn.task.exception.TaskConfigException;
import com.jy.oms.framework.asyn.task.model.TaskHandlerWrapper;
import com.jy.oms.framework.asyn.task.model.strategy.*;
import org.apache.commons.lang3.StringUtils;

/**
 * util
 *
 * @author liuquan
 */
public final class ExecuteStrategyUtil {

    /**
     * 获取任务执行规则分组标识
     */
    public static TaskExecuteStrategyEnum getTaskExecuteStrategy(Class<?> clazz) {
        if (clazz == GroupExecuteStrategy.class) {
            return TaskExecuteStrategyEnum.Group;
        } else if (clazz == LimiterExecuteStrategy.class) {
            return TaskExecuteStrategyEnum.Limiter;
        } else if (clazz == ExclusiveGroupExecuteStrategy.class) {
            return TaskExecuteStrategyEnum.Group;
        } else if (clazz == ExclusiveSameBillExecuteStrategy.class) {
            return TaskExecuteStrategyEnum.Group;
        }

        return null;
    }

    public static void checkExecuteStrategy(Object bean, ExecuteStrategyObj executeStrategyObj) {
        if (executeStrategyObj == null || StringUtils.isBlank(executeStrategyObj.getTaskCode())) {
            throw new TaskConfigException("taskCode不能为空, error ->[" + bean.getClass() + "].");
        }

        if (executeStrategyObj instanceof GroupExecuteStrategy) {
            GroupExecuteStrategy strategy = (GroupExecuteStrategy) executeStrategyObj;
            if (StringUtils.isBlank(strategy.getTaskGroup())) {
                throw new TaskConfigException("taskGoup属性不能为空, error ->[" + bean.getClass() + "].");
            }
            if (StringUtils.equalsIgnoreCase(strategy.getTaskGroup(), TaskConstants.DEFAULT_GROUP_CODE)
                    || StringUtils.equalsIgnoreCase(strategy.getTaskGroup(), TaskConstants.LIMITER_GROUP_CODE)) {
                throw new TaskConfigException("taskGoup属性不能设置为[" + TaskConstants.DEFAULT_GROUP_CODE
                        + "、" + TaskConstants.LIMITER_GROUP_CODE + "], error ->[" + bean.getClass() + "].");
            }
//            if(strategy.getTaskGroupCoreThreadNum()!=null
//                    && strategy.getTaskGroupCoreThreadNum() > TaskConstants.DEFAULT_CORE_POOL_SIZE){
//                throw new TaskConfigException("taskGroupCoreThreadNum不能设置超过[" + TaskConstants.DEFAULT_CORE_POOL_SIZE
//                        + ", error ->[" + bean.getClass() + "].");
//            }
        } else if (executeStrategyObj instanceof LimiterExecuteStrategy) {
            LimiterExecuteStrategy strategy = (LimiterExecuteStrategy) executeStrategyObj;
            if (strategy.getTimeUnit() == null || strategy.getInterval() == null || strategy.getLimiter() == null) {
                throw new TaskConfigException("timeUnit、interval、limiter属性不能为空, error ->[" + bean.getClass() + "].");
            }
        } else if (executeStrategyObj instanceof ExclusiveGroupExecuteStrategy) {
            ExclusiveGroupExecuteStrategy strategy = (ExclusiveGroupExecuteStrategy) executeStrategyObj;
            if (StringUtils.isNotBlank(strategy.getTaskGroup())) {
                if (StringUtils.equalsIgnoreCase(strategy.getTaskGroup(), TaskConstants.LIMITER_GROUP_CODE)) {
                    throw new TaskConfigException("taskGoup属性不能设置为[" + TaskConstants.LIMITER_GROUP_CODE
                            + "], error ->[" + bean.getClass() + "].");
                }
            } else {
                //默认分组
                strategy.setTaskGroup(TaskConstants.DEFAULT_GROUP_CODE);
            }

            if (StringUtils.isBlank(strategy.getTaskExclusiveGroup())) {
                //默认以单个任务为排他组
                strategy.setTaskExclusiveGroup(strategy.getTaskCode());
            }
        } else if (executeStrategyObj instanceof ExclusiveSameBillExecuteStrategy) {
            ExclusiveSameBillExecuteStrategy strategy = (ExclusiveSameBillExecuteStrategy) executeStrategyObj;
            if (StringUtils.isNotBlank(strategy.getTaskGroup())) {
                if (StringUtils.equalsIgnoreCase(strategy.getTaskGroup(), TaskConstants.LIMITER_GROUP_CODE)) {
                    throw new TaskConfigException("taskGoup属性不能设置为[" + TaskConstants.LIMITER_GROUP_CODE
                            + "], error ->[" + bean.getClass() + "].");
                }
            } else {
                //默认分组
                strategy.setTaskGroup(TaskConstants.DEFAULT_GROUP_CODE);
            }
        }
    }

    /**
     * 获取任务执行规则分组标识
     */
    public static String getTaskGroup(TaskHandlerWrapper taskHandler) {
        ExecuteStrategyObj executeStrategyObj = taskHandler.getExecuteStrategy();
        if (executeStrategyObj instanceof GroupExecuteStrategy) {
            GroupExecuteStrategy strategy = (GroupExecuteStrategy) executeStrategyObj;
            return strategy.getTaskGroup();
        } else if (executeStrategyObj instanceof LimiterExecuteStrategy) {
            return TaskConstants.LIMITER_GROUP_CODE;
        } else if (executeStrategyObj instanceof ExclusiveGroupExecuteStrategy) {
            ExclusiveGroupExecuteStrategy strategy = (ExclusiveGroupExecuteStrategy) executeStrategyObj;
            return strategy.getTaskGroup();
        } else if (executeStrategyObj instanceof ExclusiveSameBillExecuteStrategy) {
            ExclusiveSameBillExecuteStrategy strategy = (ExclusiveSameBillExecuteStrategy) executeStrategyObj;
            return strategy.getTaskGroup();
        }

        return null;
    }

    /**
     * 构造默认分组执行器策略
     */
    public static GroupExecuteStrategy buildGroupPerConsumerExecuteStrategy(String taskCode) {
        GroupExecuteStrategy strategy = new GroupExecuteStrategy(taskCode, TaskExecuteStrategyEnum.Group);
        strategy.setTaskGroup(TaskConstants.DEFAULT_GROUP_CODE);
        return strategy;
    }
}
